package com.zzxy.mybatis.test;

import com.zzxy.mybatis.entity.Order;
import com.zzxy.mybatis.ext.OrderExt;
import com.zzxy.mybatis.mapper.OrderMapper;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

public class testOrderMapper {

    InputStream is;
    SqlSessionFactory sessionFactory;
    SqlSession session;


    @Before
    public void before() throws IOException {
         is = Resources.getResourceAsStream("SqlMapConfig.xml");

        //b)通过SqlSessionFactoryBuilder创建SqlSessionFactory会话工厂。
         sessionFactory = new SqlSessionFactoryBuilder().build(is);

        // c)通过SqlSessionFactory创建SqlSession。
         session = sessionFactory.openSession();
    }

    @After
    public void after(){
        session.close();
    }




    @Test
    public void testMapperfindOrderUser(){
        OrderMapper mapper = session.getMapper(OrderMapper.class);
        OrderExt ext = mapper.findOrderUser(3);
        System.out.println(ext);//sout
    }


    @Test
    public void testMapperfindOrderUserByresultMap(){
        OrderMapper mapper = session.getMapper(OrderMapper.class);
        Order order = mapper.findOrderUserByresultMap(3);
        //输出对象自动调用toString
        System.out.println(order);//sout
        System.out.println(order.getUser());//sout
    }

    @Test
    public void testMapperfindMapOrderone2more(){
        OrderMapper mapper = session.getMapper(OrderMapper.class);
        Order order = mapper.findOrderbyMapOn2More(3);
        System.out.println(order);
    }


    @Test
    public void testMapperfindLazyLoading(){
        OrderMapper mapper = session.getMapper(OrderMapper.class);
        List<Order> orderList = mapper.findOrderUserByLazyLoading();
        for (Order order:orderList) {
            //不使用下面这个方法不会显示结果，但是会显示一共几条数据
            System.out.println(order.getUser());
            System.out.println("-------------------");
        }

    }




}
